'''

Case Type： 数据库系统
Case Name： 修改postmaster.id中pid为已存在的进程号，不能够对数据库进行正常停止
Case No. :  Opengauss_Reliability_Dbsys_Case115
Create At:  2020/06/28
@zou_jialiang0505328126
Descption:  1.启动数据库务2.停止主进程3.修改postmaster.id中pid为已存在的进程号4.停止数据库

history：
created： 2020/06/28  created by @wan005
'''
import os
import unittest
from yat.test import Node
import time
import _thread
import queue
from yat.test import macro

import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant


class ModifyPidFile(unittest.TestCase):
    RESTART_SUCCESS_MSG = Constant.RESTART_SUCCESS_MSG
    STOP_NOT_PERMIT_MSG = Constant.STOP_NOT_PERMIT_MSG
    sh = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case115.py start------------')
        self.rootNode = Node()
        self.dbUserNode = Node(node='dbuser')
        self.PID_FILE_NAME = os.path.join(macro.DB_INSTANCE_PATH, macro.PID_FILE_NAME)
        self.com = Common()
        self.DB_ENV_PATH = macro.DB_ENV_PATH

    def test_Modify_PID_File(self):
        # STOP opengauss PID
        self.log.info("STOP opengauss PID")
        # get_pid_cmd = 'lsof -i:{port} | grep gaussdb'.replace('{port}', self.rootNode.db_port)
        # 最新版本数据库进程command变更成了GaussMast，因而取消对gaussdb的匹配
        get_pid_cmd = 'lsof -i:{port}'.replace('{port}', self.rootNode.db_port)
        pid_msg = self.rootNode.sh(get_pid_cmd).result()
        self.log.info(pid_msg)
        pid = self.com.parsePidList(pid_msg)
        self.rootNode.sh('kill -9 ' + pid[0])

        # # 获取已存在的pid
        # cmd = "pidstat | tail -1"
        # self.log.info(cmd)
        # msg = self.rootNode.sh(cmd).result()
        # already_exists_pid = msg.split()[3].strip()
        # self.log.info("pid " + already_exists_pid + " already exist")

        already_exists_pid = 1
        self.com.mod_file_rows(self.dbUserNode, self.PID_FILE_NAME, 1, 1, already_exists_pid)
        result = self.dbUserNode.sh("cat " + self.PID_FILE_NAME).result()
        self.log.info(result)

        self.log.info("----------------stop opengauss-------------------------")
        stopmsg = self.dbUserNode.sh('source {};gs_om -t stop'.format(self.DB_ENV_PATH)).result()
        self.log.info(stopmsg)
        self.assertTrue(stopmsg.find(self.STOP_NOT_PERMIT_MSG) > -1)

    def tearDown(self):
        self.log.info('this is tearDown')
        self.log.info("-----------remove pid file-----------")
        self.dbUserNode.sh("rm -rf " + self.PID_FILE_NAME)
        self.log.info("-----------restart opengauss-----------")
        restartCmd = '''
            source {source_path};
            gs_ctl restart -D {DN1_PATH} -m immediate'''.format(source_path=self.DB_ENV_PATH,
                                                                DN1_PATH=macro.DB_INSTANCE_PATH)
        restartMsg = self.dbUserNode.sh(restartCmd).result()
        self.log.info(restartMsg)
        self.assertTrue(restartMsg.find(self.RESTART_SUCCESS_MSG) > -1)
        time.sleep(10)

        self.log.info("-----------start opengauss cluster-----------")
        self.sh.startDbCluster()
